package co.edu.uniandes.distribuidos.exclusion;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;

import co.edu.uniandes.distribuidos.exclusion.downloadContainer.DownloadFile;
import co.edu.uniandes.distribuidos.exclusion.downloadContainer.DownloadFileContainer;
import co.edu.uniandes.distribuidos.exclusion.exception.ListDownloadManagerException;

public class DownloadFileManager {

	private FileWriter writer;
	
	private DownloadFileContainer container;
	
	private static final String LOGFILE="logDownload.xml";
	
	private static DownloadFileManager singleton;
	
	
	
	public DownloadFileContainer getContainer() {
		return container;
	}



	public void setContainer(DownloadFileContainer container) {
		this.container = container;
	}



	public static DownloadFileManager getInstance() throws ListDownloadManagerException{
		if(singleton==null){
			singleton=new DownloadFileManager();
		}
		return singleton;
	}
	
	
	
	private DownloadFileManager() throws ListDownloadManagerException{
		init();
	}
	
	private void init() throws ListDownloadManagerException {
		
		//create file writer and a file to store the events
		
		try {
			File logFile = new File(LOGFILE);
	        if(!logFile.exists() || !logFile.isFile()){
	        	boolean success = logFile.createNewFile();
	        	if(success){
	        		this.container=new DownloadFileContainer();
	        		writeLog();
	        	}else{
	        		throw new ListDownloadManagerException("Imposible crear el archivo de log");
	        	}
	        }else{
				load();
	        }
		} catch (Exception e) {
			e.printStackTrace();
			throw new ListDownloadManagerException(e);
		}
	}
	
	private void writeLog() throws ListDownloadManagerException{
		
		try {
			File file = new File(LOGFILE);
			writer = new FileWriter(file);
			Marshaller.marshal(container, writer);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ListDownloadManagerException(e);
		}
		
	}

	public void save(DownloadFile df) throws ListDownloadManagerException{

		// Marshall the events to log
		try {
			container.add(df);
			writeLog();
		} catch (Exception e) {
			e.printStackTrace();
			throw new ListDownloadManagerException(e);
		} 
	}
	
	public void load() throws ListDownloadManagerException{

		// Unmarshall the log
		try {
			File file=new File(LOGFILE); 
			
		    Unmarshaller un = new Unmarshaller(DownloadFileContainer.class);

		    // read in the StorageInfoContainer using the mapping
		    FileReader in = new FileReader(file);
		    container = (DownloadFileContainer) un.unmarshal(in);
		    in.close();
			if(container==null){
				container=new DownloadFileContainer();
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ListDownloadManagerException(e);
		} 
		
	}
	
	public boolean existDownloadFile(){
		if(container!=null&&!container.isEmpty()){
			return true;
		}else{
			return false;
		}
		
	}
	
	
	public void remove(DownloadFile df) throws ListDownloadManagerException{
		try{
			if(df!=null){
				container.remove(df);
				writeLog();
			}
		}catch (Exception e) {
			e.printStackTrace();
			throw new ListDownloadManagerException(e);
		}
	}
	
	public void removeDowloadFiles() throws ListDownloadManagerException{
		try{
			container.removeAll();
			writeLog();
		}catch (Exception e) {
			e.printStackTrace();
			throw new ListDownloadManagerException(e);
		}
	}
	
}
